
package com.compuvest.tld;

// <editor-fold defaultstate="collapsed" desc="import">
import com.compuvest.web.Item;
import com.compuvest.web.Items;
import java.util.ArrayList;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;


public class Itemstld extends TagSupport {

    public final static String STOCKFORM = "<b>&#x95; Stock: <font color='#808080'>%s</font></b><br/>";
    public final static String PRICEFORM = "<div class='price'>$%,.2f</div>";
    public final static String RESPRICEFORM = "<div class='prices'>$%,.2f</div>";
    public final static String ADDFORM = "<a class='addc' href='%s?iidd=%d'>Add to Cart</a>";
    public final static String ADDFORMRELATIVE = "<a class='addc' href='%s?iidd=%d&relv=1'>Add to Cart</a>";
    public final static String BANNER = "<div class='mes2'>%s</div>";
    public final static String URLSPEC = "<span id='spp' class='tipz'>Special Price <img id='spp' class='tipz' src='../img/hlp.gif' alt=''/></span>";
    public final static String PRICERES = "<span class='rpr'>$%,.2f</span>";
    public final static String IMGURL = "%s%s.jpg";

    protected int categoryID;
    protected int subcategoryID;
    protected int pageNo;
    protected String pageUrl;
    protected int sortID;
    protected boolean reseller = false;
    protected boolean tail = true;
    protected Items items;
    protected StringBuilder body;

    public void setCategoryID(int categoryID)       { this.categoryID = categoryID; }
    public void setSubcategoryID(int subcategoryID) { this.subcategoryID = subcategoryID; }
    public void setPageNo(int pageNo)               { this.pageNo = pageNo; }
    public void setPageUrl(String pageUrl)          { this.pageUrl = pageUrl; }
    public void setReseller(boolean reseller)       { this.reseller = reseller; }
    public void setItems(Items items)               { this.items = items; }
    public void setSortID(int sortID)               { this.sortID = sortID; }
    public void setTailShow(boolean tail)           { this.tail = tail; }

// <editor-fold defaultstate="collapsed" desc="doStartTag()">
    @Override
    public int doStartTag() throws JspException {

	if(items == null) return SKIP_BODY;

	ArrayList<Item> itemsArray = items.getItems();

	if(!items.isEmpty()) {
	    body = new StringBuilder(25000);
	    body.append("<fieldset style='width:94%;margin:5px 0;'>");
	    body.append("<table class='tabh'>");
            body.append("<colgroup><col width='71%'><col align='right' width='23%'><col width='6%'></colgroup>");
   	    body.append("<tr><th>&nbsp;&nbsp;P r o d u c t&nbsp;&nbsp;&nbsp;N a m e&nbsp;&nbsp;</th>");
   	    body.append("<td align='right'>Sort by : ");
            body.append("<select name='srt' onChange='document.SortForm.submit();return false;'>");
            body.append("<option value='1' ").append(sortID == 1 ? "selected='yes'" : "").append(">Price: low to high</option>");
            body.append("<option value='2' ").append(sortID == 2 ? "selected='yes'" : "").append(">Price: high to low</option>");
            body.append("<option value='3' ").append(sortID == 3 ? "selected='yes'" : "").append(">Relevance</option>");
            body.append("</select>");
            body.append("</td>");
   	    body.append("<th>Price Drop</th>");
	    body.append("</tr></table></fieldset>");

	    String imgs, stock2, price2, url2;
            String httpLink = "./"; //pageContext.getServletContext().getInitParameter("http");
	    String basketURL = httpLink + "Basket.jsp";

	    body.append("<fieldset style='width:94%;'><table>");
            body.append("<colgroup><col width='10%'/><col width='84%'/><col width='6%'/></colgroup>");
	    for(Item item : itemsArray) {
		imgs = "../imgs/0/"; //pageContext.getServletContext().getInitParameter("imgs");
		stock2 = price2 = url2 = "";
//		if(item.getItemIDR() != 0) {
//		    stock2 = String.format(STOCKFORM, item.getStockR() ? "Yes" : String.valueOf(item.getQuantityR()));
//		    price2 = String.format(PRICEFORM, item.getPriceR());
//		    url2 = String.format(ADDFORM, basketURL, item.getItemIDR());
//		}
		//if(item.getRelativePrice() != 0) {
		//    stock2 = String.format(STOCKFORM, item.getRelativeStock() ? "Yes" : String.valueOf(item.getRelativeQuantity()));
		//    price2 = String.format(PRICEFORM, item.getRelativePrice());
		//    url2 = String.format(ADDFORMRELATIVE, basketURL, item.getItemID());
		//}
		body.append("<tr align='center'>");
		body.append("<td bgcolor='white'>");
		body.append("<img id='rs' onError='hideErr(this);' src='");
		body.append(String.format(IMGURL, imgs, item.getAccwareCode())).append("' width='65' height='65' alt=''/></td>");

		body.append("<td class='tbg'>");
		body.append("<table>");
                body.append("<colgroup>");
                body.append("<col width='20%' nowrap><col width='30%' nowrap/><col width='14%' nowrap/><col width='15%' nowrap/><col width='11%'/><col width='10%'/>");
                body.append("</colgroup>");
		body.append("<tr><td colspan='6' class='desc'>");
		body.append("<a class='lnit' href='").append(httpLink).append("Desc.jsp?iid=").append(item.getItemID()).append("'>").append(item.getItemName()).append("</a>");
		body.append(reseller ? items.getResellerPriceLine(item) : "");
		body.append("</td>");
		body.append("</tr>");
		body.append("<tr class='sku'>");
		body.append("<td>&#x95; SKU: ").append(item.getAccwareCode()).append("</td>");
		body.append("<td>&#x95; <b style='color:navy;'>").append(item.getConditionId() == 8 ? "New OEM" : item.getCondition()).append("</b> ");
                body.append((item.getBannerMask() & Items.COMMINGSOON) == 0 ? "" : "<font color='red'> / Ships in: 2-5 Days</b></font>").append("</td>");
		body.append("<td>");
		body.append(String.format(STOCKFORM, item.getStock() ? "Yes" : String.valueOf(item.getQuantity())));
		body.append(stock2);
		body.append("</td><td align='center' valign='top'>");
		body.append(String.format(BANNER, url2.length() == 0 ? items.getPriceMessage(item.getBannerMask()) : URLSPEC));
		body.append(url2.length() == 0 ? "" : "<span class='mes0'>Regular Price</span>");
		body.append("</td><td>");
		body.append(String.format(ADDFORM, basketURL, item.getItemID()));
		body.append(url2);
		body.append("</td><td align='center'>");
		body.append(String.format((reseller && item.getResPrice1() != 0d) ? RESPRICEFORM : PRICEFORM, item.getPrice()));
		body.append(price2);
		body.append("</td>");
		body.append("</tr></table>");
		body.append("</td>");
		body.append("<td class='tbg'><a class='drpa' href='javascript:;'").append(" id='").append(item.getItemID()).append("'><img src='../img/it/drp.png' border='0' alt=''></a></td>");
		body.append("</tr>");
	    }
	    body.append("</table></fieldset>\n");

	    // make page number
//---------
	    StringBuilder url = new StringBuilder(150).append(pageUrl).append("&srt=").append(sortID);
//	    int pages = getPageCount();
	    int pages = items.getPages();
	    if(pages != 1) {

                int page = pageNo + 1;
                int start = 1;
                int end = pages;
                boolean offSetEnd = false;

                body.append("<p style='width:70%;font-size:10pt;word-spacing:4px;color:#800000;'>Page:&nbsp;&nbsp;");
                if(pages - page > 9) {
                    end = page + 9;
                    offSetEnd = true;
                }
                if(page != 1)
                    body.append("<a href='").append(url).append("&pg=").append(page-2).append("'>&laquo;</a> ");
                if(page > 10) {
                    start = page - 9;
                    body.append("<a href='").append(url).append("&pg=0'>1</a> . . . ");
                }
                for(int i = start; i <= end; i++) {
                    if(i == page)
                        body.append("<font size='3'>").append(i).append("</font> ");
                    else
                        body.append(" <a href='").append(url).append("&pg=").append(i-1).append("'>").append(i).append("</a> ");
                }
                if(offSetEnd)
                    body.append(" . . . <a href='").append(url).append("&pg=").append(pages-1).append("'>").append(pages).append("</a> ");
                if(page != pages)
                    body.append("<a href='").append(url).append("&pg=").append(page).append("'> &raquo;</a>");

                body.append("</p><p/>");
	    }
	    else {
                body.append("<p/><p/>");
	    }
//---------
	    // save page link
	    pageContext.setAttribute("backItemsPage", url.append("&pg=").append(pageNo).toString());
	}
	else {
	    body = new StringBuilder(55);
	    if(tail)
            body.append("<h3>There were no matching result for your query.</h3>");
	}

        try {
	    pageContext.getOut().print(body.toString());
        }
        catch(java.io.IOException e) {
            throw new JspTagException("Items: " + e.getMessage());
        }
        return SKIP_BODY;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="doEndTag()">
    @Override
    public int doEndTag() {
        return EVAL_PAGE;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="release()">
    @Override
    public void release() {
	pageUrl = null;
	body = null;
	items = null;
    }
// </editor-fold>

}
